package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

@WebServlet("/updateCartJSServlet")
public class UpdateCartJsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);

    }
    private Cart cart;
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        cart = (Cart)session.getAttribute("cart");
        CatalogService catalogService = new CatalogService();
        Account account = (Account)req.getSession().getAttribute("account");

//        Iterator<CartItem> cartItemIterator = cart.getAllCartItems();
        try {
            int quantity = Integer.parseInt((String) req.getParameter("quantity"));

            String itemId = req.getParameter("whatitem");
            System.out.println(itemId);
            catalogService.updatecartquantities(itemId,quantity,account.getUsername());//持久化购物车
            cart.setQuantityByItemId(itemId, quantity);
//            if (quantity < 1) {
//               // cart.removeItemById(itemId);
//                catalogService.removecartitem(itemId,quantity,account.getUsername());
//            }
            } catch (Exception e) {
                e.printStackTrace();
                session.setAttribute("message", "The Quantities of Item must be Integer!");
                req.getRequestDispatcher(ERROR).forward(req, resp);
            }

//        while (cartItemIterator.hasNext()) {
//            //产品数量增加
//            CartItem cartItem = (CartItem) cartItemIterator.next();
//            String itemId = cartItem.getItem().getItemId();
//
//            try {
//                int quantity = Integer.parseInt((String) req.getParameter("quantity"));
//                catalogService.updatecartquantities(itemId,quantity,account.getUsername());//持久化购物车
//                cart.setQuantityByItemId(itemId, quantity);
//                if (quantity < 1) {
//                    cartItemIterator.remove();
//                    cart.removeItemById(itemId);
//                    catalogService.removecartitem(itemId,quantity,account.getUsername());
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                session.setAttribute("message", "The Quantities of Item must be Integer!");
//                req.getRequestDispatcher(ERROR).forward(req, resp);
//            }
//        }
        session.setAttribute("cart", cart);

        int quantity = Integer.parseInt((String) req.getParameter("quantity"));
        String itemId = req.getParameter("whatitem");
        Cart cart2 = (Cart)session.getAttribute("cart");
        Iterator<CartItem> cartItemIterator2 = cart2.getAllCartItems();
        String quantityAll = "";
        while (cartItemIterator2.hasNext()) {
            //产品数量增加
            CartItem cartItem2 = cartItemIterator2.next();
            int quantity2 = cartItem2.getQuantity();
            System.out.println(cartItem2.getItem().getItemId());
            if (cartItem2.getItem().getItemId().equals( itemId)){
                quantityAll = quantity + "," + cartItem2.getTotal() + "," + cart2.getSubTotal();
//                if (quantity2 < 1) {
//                    cart.removeItemById(cartItem2.getItem().getItemId());
//                    //catalogService.removecartitem(itemId,quantity2,account.getUsername());
//                }
                break;
            }else{
                System.out.println("111");
            }

        }
        System.out.println(quantityAll);
        resp.setContentType("text/xml");
        PrintWriter out = resp.getWriter();
        out.write(quantityAll);
    }
}
